<?php
function detail_kelompok_shift( $karyawan_id ){
	my_set_code_js('
		function submitChange(tanggal ,field ,value){
			location.href=\'index.php?com='.$_GET['com'].'&field=\'+field+\'&value=\'+value+\'&tanggal=\'+tanggal+\'&task=update&karyawan_id='.$karyawan_id.'\'  ;
		}
	');

	$karyawan = loaddata_karyawan($karyawan_id , false);
	$periode_id =get_periode_absen();
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = $periode['tahun'] ;
	$kelompok_shift_id=$karyawan['kelompok_shift_id'] ;
	if($bulan =='1' ){
		$startdate = ($tahun -1).'-12-11';
		$middledata = ($tahun -1).'-12-25';
		$middledata2 = ($tahun -1).'-12-26';
		$enddate = $tahun.'-'.sprintf( '%02d',$bulan) .'-10'; //hasilnya 10
	}else{
		$startdate = $tahun.'-'.sprintf( '%02d',$bulan-1) .'-11';
		$middledata =  $tahun .'-'.sprintf( '%02d',$bulan-1) .'-25';
		$middledata2 =  $tahun .'-'.sprintf( '%02d',$bulan-1) .'-26';
		$enddate =$tahun.'-'.sprintf( '%02d',$bulan) .'-10'; //hasilnya 10
	}
	
	$tanggals = list_kalender($startdate , $middledata  );
	$tanggals = array_unique($tanggals) ;
	
	$header = array(
		'DT'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'S. Sts'=>array('style'=>'text-align:left;border-bottom:2px solid;width:15%'),   
		'S. Time'=>array('style'=>'text-align:left;border-bottom:2px solid;width:23%'),   
		'RS'=>array('style'=>'text-align:left;border-bottom:2px solid;width:15%'),   
		'PC'=>array('style'=>'text-align:left;border-bottom:2px solid;width:14%'),   
		'R Time'=>array('style'=>'text-align:left;border-bottom:2px solid;width:23%'),   
		'M'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),   
		'P'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),   
	);
	$row= array();
	$waktu_kerja_id= (int) isset( $karyawan['waktu_kerja_id'] )? $karyawan['waktu_kerja_id'] : 0 ;  
	foreach($tanggals as $tanggal){
		$datas=get_realisasi($tanggal , 
			$karyawan_id, 
			$waktu_kerja_id,
			$karyawan['kelompok_shift_id'],
			$karyawan['kelompok'] ) ;	
		if(get_hari_libur($tanggal)){	
			$row[] = array(
				'tanggal'	=>position_text_align('<font color="red">'.date('d',strtotime($tanggal)).'</font>','center'),
				's_jadwal'	=>$datas['s_jadwal_kerja'], 
				's_time' 	=>$datas['s_time'], 
				'r_status' 	=>$datas['r_status'], 
				'r_pg' 		=>$datas['r_pg'], 
				'r_time'	=>$datas['r_time'], 
				'chk1'		=>$datas['fr_jm'], 
				'chk2'		=>$datas['fr_jp'],
			);
		}else{	
			$row[] = array(
				'tanggal'	=>position_text_align(date('d',strtotime($tanggal)),'center'),
				's_jadwal'	=>$datas['s_jadwal_kerja'], 
				's_time' 	=>$datas['s_time'], 
				'r_status' 	=>$datas['r_status'], 
				'r_pg' 		=>$datas['r_pg'], 
				'r_time'	=>$datas['r_time'], 
				'chk1'		=>$datas['fr_jm'], 
				'chk2'		=>$datas['fr_jp'],
			);
		}	
	}
	$datas = table_rows($row);
	$table1 = table_builder($header , $datas ,  2 , false);
	
	$tanggals = list_kalender($middledata2 ,$enddate  );
	$tanggals = array_unique($tanggals) ;
	$row= array(); 
	foreach($tanggals as $tanggal){
		$datas=get_realisasi($tanggal , 
			$karyawan_id, 
			$waktu_kerja_id,
			$karyawan['kelompok_shift_id'],
			$karyawan['kelompok'] ) ;	
		if(get_hari_libur($tanggal)){	
			$row[] = array(
				'tanggal'	=>position_text_align('<font color="red">'.date('d',strtotime($tanggal)).'</font>','center'),
				's_jadwal'	=>$datas['s_jadwal_kerja'], 
				's_time' 	=>$datas['s_time'], 
				'r_status' 	=>$datas['r_status'], 
				'r_pg' 		=>$datas['r_pg'], 
				'r_time'	=>$datas['r_time'], 
				'chk1'		=>$datas['fr_jm'], 
				'chk2'		=>$datas['fr_jp'],
			);
		}else{	
			$row[] = array(
				'tanggal'	=>position_text_align(date('d',strtotime($tanggal)),'center'),
				's_jadwal'	=>$datas['s_jadwal_kerja'], 
				's_time' 	=>$datas['s_time'], 
				'r_status' 	=>$datas['r_status'], 
				'r_pg' 		=>$datas['r_pg'], 
				'r_time'	=>$datas['r_time'], 
				'chk1'		=>$datas['fr_jm'], 
				'chk2'		=>$datas['fr_jp'],
			);
		}	
	}
	$datas = table_rows($row);
	$table2 = table_builder($header , $datas ,  2 , false);
 
	$newline = array(
		'Periode'=>sprintf( '%02d',$bulan).'-'.$tahun ,
		'Tipe Waktu Kerja'=>$karyawan['waktu_kerja'],
		'Kelompok'=>$karyawan['kelompok'],
	);
	$view = karyawan_header_detail($karyawan  ,  true , $newline );
	 
	$split ='<div style="height:470px;"><form method="post" name="multiDataBox">
	<div style="width:49%;float:left">'.$table1.'</div>';
	$split .='<div style="width:49%;float:right">'.$table2.'</div></div>';
	$view .= '<div style="width:98%;text-align:right;">
	<!--input type="button" value="Lihat Log" class="main_button" onclick="javascript:location.href=\'index.php?com=rpt_realisasi_wk&task=absenperkaryawan&karyawan_id='.$karyawan_id.'\'"/ -->
	<!-- input type="button" value="Cetak" class="main_button" onclick="javascript:window.open(\'index.php?com=report&task=rpt_wk_realisasi&karyawan_id='.$karyawan_id.'&'.rand(0,1111).'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');"/ -->
	<input type="button" value="Back" class="main_button" onclick="javascript:location.href=\'index.php?com=realisasi_wk_periode\'"/>
	</form></div>'   ; 	 	
	return $view . $split;
}
 
function get_realisasi($tanggal , $karyawan_id, $waktu_kerja_id, $kelompok_shift_id, $kelompok ){
	$datas=array();
	list($yyyy,$mm,$dd) = explode("-" , $tanggal);
	$dd = (int) $dd;
	$ch = "SELECT * FROM a_realisasi 
		WHERE karyawan_id = {$karyawan_id} 
			AND tgl_index='{$dd}' 
			ORDER BY realisasi_id 
			DESC LIMIT 1";
	$chr = my_query($ch);
	$row = my_fetch_array($chr); 
	$flag_masuk=$row['flag_masuk'] ;
	$flag_pulang=$row['flag_pulang'] ;
	
	if($flag_masuk=='Y'){ 
		$check_jm = array(
			'name'=>'check_jm_'.$tanggal,
			'id'=>'check_jm_'.$tanggal,
			'onchange'=>'submitChange(\''.'tgl_'.$dd.'\' , \'flag_masuk\'  ,\'T\',\''.$tanggal.'\' )',
			'type'=>'checkbox',
			'checked'=>'checked'
		);
	}else{
		$check_jm = array(
			'name'=>'check_jm_'.$tanggal,
			'id'=>'check_jm_'.$tanggal,
			'onchange'=>'submitChange(\''.'tgl_'.$dd.'\' , \'flag_masuk\'  ,\'Y\',\''.$tanggal.'\' )',
			'type'=>'checkbox',
		); 
	}
	$form_check_jm = form_dynamic($check_jm);
	$datas['fr_jm']=$form_check_jm ;	

	if($flag_pulang=='Y'){ 
		$check_jp = array(
			'name'=>'jam_pulang_'.$tanggal,
			'id'=>'jam_pulang_'.$tanggal,
			'onchange'=>'submitChange(\''.'tgl_'.$dd.'\' , \'flag_pulang\'  ,\'T\',\''.$tanggal.'\' )',
			'type'=>'checkbox',
			'checked'=>'checked'
		);
	}else{
		$check_jp = array(
			'name'=>'jam_pulang_'.$tanggal,
			'id'=>'jam_pulang_'.$tanggal,
			'onchange'=>'submitChange(\''.'tgl_'.$dd.'\' , \'flag_pulang\'  ,\'Y\',\''.$tanggal.'\' )',
			'type'=>'checkbox',
		); 
	}
	$form_check_jp = form_dynamic($check_jp);
	$datas['fr_jp']=$form_check_jp ;		

	$jam_datang=$row['jam_datang'] ; 
	if($jam_datang=='00:00:00'){
		$jam_datang='';
	}	
	$jam_datang=(trim($jam_datang)<>'') ? date('H:i',strtotime($jam_datang)) :'';

	$jam_pulang=$row['jam_pulang'] ;
	if($jam_pulang=='00:00:00'){
		$jam_pulang='';
	}	
	$jam_pulang=(trim($jam_pulang)<>'') ? date('H:i',strtotime($jam_pulang)) :'';
	$datas['r_time']=$jam_datang.'-'.$jam_pulang ;
	$sc_jadwal_kerja_kelompok_id=(int) $row['sc_jadwal_kerja_kelompok_id'];
	if($sc_jadwal_kerja_kelompok_id<=0){  //ambil schedule
		if($waktu_kerja_id>3){
			$kelompok=get_kelompok_shift_jadwal($tanggal, $kelompok_shift_id );		
		}else{
			if(get_hari_libur($tanggal)){	
				$kelompok='X';
			}
		}
		if($kelompok=='X'){
			$sc_jadwal_kerja_kelompok_id=21 ;
		}else{	
			$query = "SELECT * FROM a_jadwal_kerja_kelompok 
					WHERE waktu_kerja_id = {$waktu_kerja_id}
					AND kelompok='{$kelompok}' ";
			$res = my_query($query);
			$reals = my_fetch_array($res);
			$sc_jadwal_kerja_kelompok_id=(int) $reals['jadwal_kerja_kelompok_id'];		
		}
	}	
	
	$status_absen_id=get_status_absen_id($karyawan_id , $tanggal );
	if($sc_jadwal_kerja_kelompok_id==21){	
		$datas['s_jadwal_kerja']= 'X';
		if($status_absen_id<>15){  //BT
			$status_absen_id=2;
		}	
	}else{
		$query = "SELECT b.label,a.kelompok,a.waktu_kerja_id FROM a_jadwal_kerja_kelompok a 
			INNER JOIN a_waktu_kerja b ON a.waktu_kerja_id=b.waktu_kerja_id 
			WHERE a.jadwal_kerja_kelompok_id=".$sc_jadwal_kerja_kelompok_id." 
			ORDER BY a.jadwal_kerja_kelompok_id ";
		$jadwal_kerjas = my_fetch_array(my_query($query));	
		$waktu_kerja_id=$jadwal_kerjas['waktu_kerja_id'] ;
		$kelompok=$jadwal_kerjas['kelompok'] ;
		$datas['s_jadwal_kerja']=$jadwal_kerjas['label'].'/'.$kelompok ;
	}
	if($datas['s_jadwal_kerja']== 'X'){
		$datas['s_time']='' ;	
	}else{
		$hari_id=get_hari_id_by_date($tanggal);
		if(is_null($waktu_kerja_id)){
			$datas['s_time']='-' ;		
		}else{
			$query = "SELECT jam_masuk,jam_pulang FROM a_jadwal_kerja 
					WHERE waktu_kerja_id =".$waktu_kerja_id."  
					AND kelompok='".$kelompok."' 
					AND hari_id={$hari_id}";
			$jams = my_fetch_array(my_query($query));	
			$sc_jam_datang=$jams['jam_masuk'] ;
			$sc_jam_datang=(trim($sc_jam_datang)<>'') ? date('H:i',strtotime($sc_jam_datang)) :'';
			$sc_jam_pulang=$jams['jam_pulang'] ;
			$sc_jam_pulang=(trim($sc_jam_pulang)<>'') ? date('H:i',strtotime($sc_jam_pulang)) :'';
			$datas['s_time']=$sc_jam_datang.'-'.$sc_jam_pulang ;		
		}	
	}
	if(	$status_absen_id==0){
		if($waktu_kerja_id > 3){  //shift
			if($kelompok=='X'){
				$status_absen_id=2 ; //X
			}else{
				$status_absen_id=(int) $kelompok+2 ; //1,2,3				
			}
		}else{ //non shift
			if(get_hari_libur($tanggal)){	
				$status_absen_id=2 ; //X
			}else{
				$status_absen_id=1 ; //N
			}	
		}
		if($status_absen_id<>2 
			AND trim($jam_datang)=='' 
			AND trim($jam_pulang)==''
			AND !($row['flag_masuk']=='Y' OR $row['flag_pulang']=='Y')
			){
			$status_absen_id=16 ; //A
		}	
	}
	$telat='' ;
	$statuss=my_get_data_by_id('a_status_absen','status_absen_id',$status_absen_id) ;
	//$datas['r_status']=	$statuss['initial_absen'] ;		
	$datas['r_status']=	$row['status_absen'] ;		
	if(	$statuss['hari_kerja']=="Y"){
		if( trim($jam_datang)=='' AND $row['flag_masuk']<>'Y'  ){
			$datas['r_status']='A' ; ////***********
		}elseif( trim($jam_pulang)=='' AND $row['flag_pulang']<>'Y'  ){
			$datas['r_status']='A' ; ////***********
		}else{	
			$jam_sc1=(trim($sc_jam_datang)=='' ? 0 : strtotime($sc_jam_datang.':00'));
			$jam_sc2=(trim($sc_jam_pulang)=='' ? 0 : strtotime($sc_jam_pulang.':00'));
			$jam_1=(trim($jam_datang)=='' ? 0 : strtotime($jam_datang.':00'));
			$jam_2=(trim($jam_pulang)=='' ? 0 : strtotime($jam_pulang.':00'));
			
			if($jam_sc1 > $jam_sc2){ //lintas tanggal
				
			}else{
				$jam_menit=0;
				if( $jam_1>$jam_sc1 AND $row['flag_masuk']<>'Y'){
						$jam_menit=$jam_1-$jam_sc1;
				}
				if( $jam_sc2>$jam_2 AND $row['flag_pulang']<>'Y'){
						$jam_menit+=($jam_sc2-$jam_2);
				}	
				if($jam_menit>0){
					$telat=gmdate("H:i",$jam_menit );
				}	
			}
		}	

	}

	$datas['r_pg']=$telat;
	return $datas;	
}


function get_status_absen_id($karyawan_id , $date  ){

	//cuti
	$query="SELECT kelompok_cuti_id FROM surat_cuti 
		WHERE tanggal_mulai <='{$date}' 
		AND tanggal_selesai >='{$date}' 
		AND karyawan_id ={$karyawan_id}";	
	$result = my_query($query);
	if( my_num_rows($result) > 0 ){
		$statuss=my_fetch_array(my_query($query));	
		return $statuss['kelompok_cuti_id'];
	}
	
	//surat ijin
	$query="SELECT status_absen_id FROM a_surat_ijin 
		WHERE full_day='Y' 
		AND tanggal ='{$date}' 
		AND karyawan_id ={$karyawan_id}";	
	$result = my_query($query);
	if( my_num_rows($result) > 0 ){
		$statuss=my_fetch_array(my_query($query));	
		return $statuss['status_absen_id'];
	}	

	//spd
	$query="SELECT * FROM spd
		WHERE '".$date."' BETWEEN tanggal_mulai AND tanggal_selesai 
		AND tipe_karyawan='Karyawan' 
		AND karyawan_id ={$karyawan_id}
	";
	$result = my_query($query);
	if( my_num_rows($result) > 0 ){
		return 15 ; //sementara hardcore dulu
	}	
	

	//tukar dinas
	$query="SELECT * FROM a_tukar_shift 
		WHERE tanggal  ='{$date}' 
		AND (karyawan_id ={$karyawan_id} OR tukar_karyawan_id ={$karyawan_id})
	";
	$result = my_query($query);
	if( my_num_rows($result) > 0 ){
		$tukar_shifts=my_fetch_array(my_query($query));	
		$tukar_shift_id=$tukar_shifts['tukar_shift_id'] ;
		$query="SELECT pindah_jadwal_kerja_kelompok_id FROM a_tukar_shift_detail 
			WHERE tukar_shift_id={$tukar_shift_id} 
			AND karyawan_id ={$karyawan_id}
		";
		$tukar_shift_ds=my_fetch_array(my_query($query));
		$jadwal_kerja_kelompok_id=$tukar_shift_ds['pindah_jadwal_kerja_kelompok_id'];
		
		// convert dari jadwal_kerja_kelompok_id menjadi status_absen_id
		$query = "SELECT a.kelompok,a.waktu_kerja_id FROM a_jadwal_kerja_kelompok a 
			WHERE a.jadwal_kerja_kelompok_id=".$jadwal_kerja_kelompok_id." 
			ORDER BY a.jadwal_kerja_kelompok_id ";
		$jadwal_kerjas = my_fetch_array(my_query($query));	
		$waktu_kerja_id=$jadwal_kerjas['waktu_kerja_id'] ;
		$kelompok=$jadwal_kerjas['kelompok'] ;
		if($waktu_kerja_id > 3){  //shift
			if($kelompok=='X'){
				$status_absen_id=2 ; //X
			}else{
				$status_absen_id=(int) $kelompok+2 ; //1,2,3
			}
		}else{ //non shift
			if(get_hari_libur($date)){	
				$status_absen_id=2 ; //X
			}else{
				$status_absen_id=1 ; //N
			}	
		}
		return $status_absen_id;	
	}	
	return 0;	
}

function get_hari_id_by_date($date){
	$hari_id=date("N",strtotime($date) ); //Monday to Sunday
	//konversi ke Sunday to Saturday
	if($hari_id==7){
		$hari_id=1;
	}else{
		$hari_id++;
	}		
	return $hari_id;
}

function get_kelompok_shift_jadwal($tanggal , $kelompok_shift_id ){
	$periode_id=get_periode_absen();
	list($yyyy,$mm,$dd) = explode("-" , $tanggal);
	$query = "SELECT tgl_{$dd} FROM a_kalendar_shift 
		WHERE periode_id = {$periode_id} 
		AND kelompok_shift_id={$kelompok_shift_id}";
	$result = my_query($query);
	$re = my_fetch_array($result);
	return $re['tgl_'.$dd];
}

function get_hari_libur($date){
	if(is_sabtu_minggu($date)){
		return true;
	}
	$query="SELECT hari_libur_id FROM a_hari_libur
		WHERE tanggal = '{$date}'";
	if (my_num_rows( my_query($query)) >0){	
		return true;
	}	
	return false;
}

function update_jadwal( $tanggal , $field , $value, $karyawan_id ){
	$query="SELECT realisasi_id FROM a_realisasi
		WHERE karyawan_id = {$karyawan_id} AND
		field_tgl = '{$tanggal}'";
	if (my_num_rows( my_query($query)) >0){
		$query = "UPDATE a_realisasi SET 
			{$field} ='{$value}'
			WHERE karyawan_id = {$karyawan_id} AND
			field_tgl = '{$tanggal}'";				
	}else{		
		$query = "INSERT INTO a_realisasi SET
			karyawan_id = {$karyawan_id} , {$field} ='{$value}',
			field_tgl = '{$tanggal}' ";
	}
	my_query($query);		
	return true;
}
//		function submitChange(tanggal ,field ,value,formname){
//		function submitChange(tanggal ,field ,value,formname){
//submitChange('tgl_11' , 'jam_datang'  ,'07:30','2011-04-11' )		
